package com.terrace.db.system.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.terrace.db.constant.DBType;
import com.terrace.db.system.dao.LogDao;
import com.terrace.db.system.dao.RoleDao;
import com.terrace.db.system.dao.UserDao;
import com.terrace.db.system.dao.UserRoleDao;
import com.terrace.db.system.mapper.entity.Organization;
import com.terrace.db.system.mapper.entity.Role;
import com.terrace.db.system.mapper.entity.User;
import com.terrace.core.common.model.UserInfo;
import com.terrace.db.system.service.UserService;
import com.terrace.core.mybatis.entity.Pagination;
import com.terrace.core.mybatis.plugin.pagination.PaginationHelper;
import com.terrace.core.utils.ApplicationContextUtil;
import com.terrace.core.spring.datasource.annotation.DataSource;

@Service
@Transactional
@DataSource(name = DBType.ADMIN)
public class UserServiceImpl implements UserService {
	
	@Autowired
	private RoleDao roleDao;
	
	@Autowired
	private UserDao userDao;
	
	@Override
	public int saveUserSelective(User user) {
		return userDao.saveUserSelective(user);
	}

	@Override
	public int deleteUserById(String userId) {
		UserRoleDao urdao = ApplicationContextUtil.getContext().getBean(UserRoleDao.class);
		LogDao logDao = ApplicationContextUtil.getContext().getBean(LogDao.class);
		urdao.deleteByUserId(userId);
		logDao.deleteByUserId(userId);
		return userDao.deleteUserById(userId);
	}
	
	@Override
	public int updateUserSelectiveById(User user) {
		return userDao.updateUserSelectiveById(user);
	}
	
	@Override
	public User selectUserById(String userId) {
		return userDao.selectUserById(userId);
	}
	
	@Override
	public User selectUserByAccount(String userAccount) {
		return userDao.selectUserByAccount(userAccount);
	}

	@Override
	public List<Role> selectRoleByUserId(String userId) {
		return roleDao.selectRoleByUserId(userId);
	}
	
	@Override
	public UserInfo selectUserInfoByAccount(String userAccount) {
		return userDao.selectUserInfoByAccount(userAccount);
	}

	@Override
	public Pagination<User> selectUserForPagination(Pagination<User> pagination, User user) {
		PaginationHelper.prepare(pagination);
		return PaginationHelper.execute(userDao.selectUserForPagination(user));
	}

	@Override
	public Pagination<User> selectUsersInRoleForPagination(Pagination<User> pagination, User user, Role role) {
		PaginationHelper.prepare(pagination);
		return PaginationHelper.execute(userDao.selectUsersInRoleForPagination(user, role));
	}

	@Override
	public Pagination<User> selectNonExistUsersInRoleForPagination(Pagination<User> pagination, String roleId, User user) {
		PaginationHelper.prepare(pagination);
		return PaginationHelper.execute(userDao.selectNonExistUsersInRoleForPagination(roleId, user));
	}

	@Override
	public Pagination<User> selectUsersInOrgForPagination(Pagination<User> pagination, User user, Organization org) {
		PaginationHelper.prepare(pagination);
		return PaginationHelper.execute(userDao.selectUsersInOrgForPagination(user, org));
	}

	@Override
	public Pagination<User> selectUsersNotInOrgForPagination(Pagination<User> pagination, User user) {
		PaginationHelper.prepare(pagination);
		return PaginationHelper.execute(userDao.selectUsersNotInOrgForPagination(user));
	}

}
